{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Final Value of Variable After Performing Operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: finalValueAfterOperations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #执行操作后的变量值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>存在一种仅支持 4 种操作和 1 个变量 <code>X</code> 的编程语言：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>++X</code> 和 <code>X++</code> 使变量 <code>X</code> 的值 <strong>加</strong> <code>1</code></li>\n",
    "\t<li><code>--X</code> 和 <code>X--</code> 使变量 <code>X</code> 的值 <strong>减</strong> <code>1</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>最初，<code>X</code> 的值是 <code>0</code></p>\n",
    "\n",
    "<p>给你一个字符串数组 <code>operations</code> ，这是由操作组成的一个列表，返回执行所有操作后，<em> </em><code>X</code> 的 <strong>最终值</strong> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>operations = [\"--X\",\"X++\",\"X++\"]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>操作按下述步骤执行：\n",
    "最初，X = 0\n",
    "--X：X 减 1 ，X =  0 - 1 = -1\n",
    "X++：X 加 1 ，X = -1 + 1 =  0\n",
    "X++：X 加 1 ，X =  0 + 1 =  1\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>operations = [\"++X\",\"++X\",\"X++\"]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>操作按下述步骤执行： \n",
    "最初，X = 0\n",
    "++X：X 加 1 ，X = 0 + 1 = 1\n",
    "++X：X 加 1 ，X = 1 + 1 = 2\n",
    "X++：X 加 1 ，X = 2 + 1 = 3\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>operations = [\"X++\",\"++X\",\"--X\",\"X--\"]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>操作按下述步骤执行：\n",
    "最初，X = 0\n",
    "X++：X 加 1 ，X = 0 + 1 = 1\n",
    "++X：X 加 1 ，X = 1 + 1 = 2\n",
    "--X：X 减 1 ，X = 2 - 1 = 1\n",
    "X--：X 减 1 ，X = 1 - 1 = 0\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= operations.length &lt;= 100</code></li>\n",
    "\t<li><code>operations[i]</code> 将会是 <code>\"++X\"</code>、<code>\"X++\"</code>、<code>\"--X\"</code> 或 <code>\"X--\"</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [final-value-of-variable-after-performing-operations](https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [final-value-of-variable-after-performing-operations](https://leetcode.cn/problems/final-value-of-variable-after-performing-operations/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"--X\",\"X++\",\"X++\"]', '[\"++X\",\"++X\",\"X++\"]', '[\"X++\",\"++X\",\"--X\",\"X--\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        res = 0\n",
    "        n = len(operations)\n",
    "        for i in operations:\n",
    "            if i == \"++X\" or i == \"X++\":\n",
    "                res += 1\n",
    "        result = res*2 - n\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if '+' in operation else -1   for operation in  operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if op[1] == '+' else -1 for op in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X=0\n",
    "        for i in operations:\n",
    "            if i ==\"--X\" or i == \"X--\":\n",
    "                X -= 1\n",
    "            else: \n",
    "                X += 1\n",
    "    \n",
    "        return X "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if a[1] == '+' else -1 for a in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0\n",
    "        for operation in operations:\n",
    "            if operation == \"++X\" or operation == \"X++\":\n",
    "                X += 1\n",
    "            elif operation == \"--X\" or operation == \"X--\":\n",
    "                X -= 1\n",
    "        return X\n",
    "       \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for a in operations:\n",
    "            if a[1] == \"+\":\n",
    "                x += 1\n",
    "            else:\n",
    "                x -= 1\n",
    "        return x "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if op[1] == '+' else -1 for op in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        res = 0\n",
    "        for op in operations:\n",
    "            if op == '++X' or op == 'X++':\n",
    "                res += 1\n",
    "            else:\n",
    "                res -= 1\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "       return sum(map(lambda x:1 if x[1] =='+' else -1,operations))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "            forward = 0\n",
    "            for op in operations:\n",
    "                if \"++\" in op:\n",
    "                    forward += 1\n",
    "                else:\n",
    "                    forward -= 1\n",
    "            return forward "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if i == \"--X\" or i == \"X--\":\n",
    "                x -= 1\n",
    "            elif i == \"++X\" or i == \"X++\":\n",
    "                x += 1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        num = 0\n",
    "        for op in operations:\n",
    "            if op == '++X' or op == 'X++':\n",
    "                num += 1\n",
    "            else:\n",
    "                num -= 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x=0\n",
    "        for i in operations:\n",
    "            if i[1:2] =='+':\n",
    "                x+=1\n",
    "            else :\n",
    "                 x-=1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        num = 0\n",
    "        for op in operations:\n",
    "            if op == '++X' or op == 'X++':\n",
    "                num += 1\n",
    "            else:\n",
    "                num -= 1\n",
    "        return num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(operations)):\n",
    "            if operations[i] == \"X--\" or operations[i] == \"--X\":\n",
    "                ans -= 1\n",
    "            else:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if i == '++X' or i == 'X++':\n",
    "                x += 1\n",
    "            else:\n",
    "                x -= 1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if op[1] == '+' else -1 for op in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if s[1] == '+' else -1 for s in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return operations.count(\"X++\")+operations.count(\"++X\")-operations.count(\"X--\")-operations.count(\"--X\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x=0\n",
    "        for a in operations:\n",
    "            if \"+\" in a:\n",
    "                x+=1\n",
    "            else:\n",
    "                x-=1\n",
    "        return x\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        val = 0\n",
    "        for operation in operations:\n",
    "            if operation in ['++X', 'X++']:\n",
    "                val += 1\n",
    "            else:\n",
    "                val -= 1\n",
    "        return val\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        sum = 0\n",
    "        for o in operations:\n",
    "            if (\"+\") in o:\n",
    "                sum += 1\n",
    "            else:\n",
    "                sum -= 1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        res = 0\n",
    "        for o in operations:\n",
    "            if \"++\" in o:\n",
    "                res += 1\n",
    "            elif \"--\" in o:\n",
    "                res -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if i == '++X' or i == 'X++':\n",
    "                x+=1\n",
    "            if i == '--X' or i == 'X--':\n",
    "                x-=1\n",
    "        return x \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for i in operations:\n",
    "            if '+' in i:\n",
    "                ans += 1\n",
    "            else:\n",
    "                ans -= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for o in operations:\n",
    "            if '-' in o:\n",
    "                x -= 1\n",
    "            else:\n",
    "                x += 1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for operation in operations:\n",
    "            if operation[1] == '+':\n",
    "                ans += 1\n",
    "            else:\n",
    "                ans -= 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0\n",
    "        for i in operations:\n",
    "            if \"-\" in i:\n",
    "                X = X-1\n",
    "            else:\n",
    "                X = X+1\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for operation in operations:\n",
    "            if '+' in operation:\n",
    "                x += 1\n",
    "            elif '-' in operation:\n",
    "                x -= 1\n",
    "            else:\n",
    "                continue\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "      return sum(1 if \"++\" in op else -1 for op in operations)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if i == \"--X\" or i == \"X--\":\n",
    "                x -= 1\n",
    "            else:\n",
    "                x += 1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for op in operations:\n",
    "            if op[1]== '+':\n",
    "                x += 1\n",
    "            else:\n",
    "                x -= 1\n",
    "        \n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans=0\n",
    "        for i in operations:\n",
    "            if i == \"++X\" or i==\"X++\":\n",
    "                ans+=1\n",
    "            else:\n",
    "                ans-=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X=0\n",
    "        for i in operations:\n",
    "            if i=='++X' or i=='X++':\n",
    "                X+=1\n",
    "            if i=='--X' or i=='X--':\n",
    "                X-=1\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if i[1] == '+':\n",
    "                x += 1\n",
    "            else:\n",
    "                x -=1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        n = len(operations)\n",
    "        X = 0\n",
    "        for i in range(n):\n",
    "            if operations[i] == '++X' or operations[i] == 'X++':\n",
    "                X += 1\n",
    "                continue\n",
    "            if operations[i] == '--X' or operations[i] == 'X--':\n",
    "                X -= 1\n",
    "                continue\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ssx = 0\n",
    "        ppx = 0\n",
    "        for i in range(0, len(operations)):\n",
    "            if (operations[i] == \"--X\") or (operations[i] == \"X--\"):\n",
    "                ssx += 1\n",
    "            else:\n",
    "                ppx += 1\n",
    "        \n",
    "        return ppx - ssx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if i == 'X++' or i == '++X':\n",
    "                x += 1\n",
    "            if i == '--X' or i == 'X--':\n",
    "                x -= 1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if '+' in op else -1 for op in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0\n",
    "        for i in operations:\n",
    "            if i == \"++X\" or i == \"X++\":\n",
    "                X += 1\n",
    "            if i == \"--X\" or i == \"X--\":\n",
    "                X -= 1\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if i[0]==\"+\" or i[-1]==\"+\":\n",
    "                x+=1\n",
    "            else:\n",
    "                x-=1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if i[0]==\"+\" or i[-1]==\"+\":\n",
    "                x+=1\n",
    "            else:\n",
    "                x-=1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for i in range(len(operations)):\n",
    "            if operations[i][1] == '+':\n",
    "                ans += 1\n",
    "            else:\n",
    "                ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X=0\n",
    "        for i in operations:\n",
    "            if \"--\" in i: \n",
    "                X -= 1\n",
    "            else: \n",
    "                X += 1\n",
    "        return X "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if op[1]=='+' else -1 for op in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if op[1] == '+' else -1 for op in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for oper in operations:\n",
    "            if '++' in oper:\n",
    "                ans += 1\n",
    "            else:\n",
    "                ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X=0\n",
    "        for i in range(len(operations)):\n",
    "            if operations[i]==\"--X\" or operations[i]==\"X--\":\n",
    "                X-=1\n",
    "            else:\n",
    "                X+=1\n",
    "        return X             \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if op[1] == '+' else -1 for op in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if \"++\" in i:x += 1\n",
    "            else:x -= 1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for s in operations:\n",
    "            if s[1] == \"+\":\n",
    "                ans += 1\n",
    "            else:\n",
    "                ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X=0\n",
    "        for i in operations:\n",
    "            if i ==\"--X\" or i == \"X--\":\n",
    "                X -= 1\n",
    "            else: \n",
    "                X += 1\n",
    "        \n",
    "        return X "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for op in operations:\n",
    "            x += 1 if op[1] == '+' else -1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x = 0\n",
    "        for i in operations:\n",
    "            if \"++\" in i:\n",
    "                x += 1\n",
    "            else:\n",
    "                x -= 1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0\n",
    "        for i in operations:\n",
    "            if i in ['++X' ,'X++']:\n",
    "                X += 1\n",
    "            else:\n",
    "                X -= 1\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        res = 0\n",
    "        for i in operations:\n",
    "            if '-' in i:\n",
    "                res -= 1\n",
    "            else:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0  # 初始值为 0\n",
    "\n",
    "        for op in operations:\n",
    "            if \"++\" in op:\n",
    "                X += 1\n",
    "            elif \"--\" in op:\n",
    "                X -= 1\n",
    "\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        n = len(operations)\n",
    "        X = 0\n",
    "        for i in range(n):\n",
    "            if operations[i] == '++X' or operations[i] == 'X++':\n",
    "                X += 1\n",
    "                continue\n",
    "            if operations[i] == '--X' or operations[i] == 'X--':\n",
    "                X -= 1\n",
    "                continue\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        res = 0\n",
    "        for operation in operations:\n",
    "            if operation[1] == \"+\":\n",
    "                res += 1\n",
    "            else:\n",
    "                res -= 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0\n",
    "        for op in operations:\n",
    "            if op[1] == '+':\n",
    "                X = X + 1\n",
    "            else:\n",
    "                X = X - 1\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        sum=0\n",
    "        for i in operations:\n",
    "            if i == 'X++' or i == '++X':\n",
    "                sum += 1\n",
    "            else:\n",
    "                sum -= 1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans = 0\n",
    "        for o in operations:\n",
    "            if o == \"X++\" or o == \"++X\":\n",
    "                ans += 1\n",
    "            else:\n",
    "                ans -= 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0\n",
    "        for i in operations:\n",
    "            if i[1] == '-':\n",
    "                X -= 1\n",
    "            else:\n",
    "                X += 1\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        n = len(operations)\n",
    "        X = 0\n",
    "        for i in range(n):\n",
    "            if operations[i] == '++X' or operations[i] == 'X++':\n",
    "                X += 1\n",
    "                \n",
    "            if operations[i] == '--X' or operations[i] == 'X--':\n",
    "                X -= 1\n",
    "                \n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if s[1] == '+' else -1 for s in operations)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        ans=operations.count(\"++X\")+operations.count(\"X++\")-operations.count(\"--X\")-operations.count(\"X--\")\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X=0\n",
    "        for i in operations:\n",
    "            if i =='--X' or i=='X--':\n",
    "                X-=1\n",
    "            elif i =='++X' or i== 'X++':\n",
    "                X+=1\n",
    "        return X\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0\n",
    "        for i in operations:\n",
    "            if '-' in i:\n",
    "                X -= 1\n",
    "            else:\n",
    "                X += 1\n",
    "        return X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        return sum(1 if '+' in s else -1 for s in operations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        x=0\n",
    "        for i in operations:\n",
    "            if i==\"--X\" or i==\"X--\":\n",
    "                x-=1\n",
    "            else:\n",
    "                x+=1\n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def finalValueAfterOperations(self, operations: List[str]) -> int:\n",
    "        X = 0  # 初始值为 0\n",
    "\n",
    "        for op in operations:\n",
    "            if \"++\" in op:\n",
    "                X += 1\n",
    "            elif \"--\" in op:\n",
    "                X -= 1\n",
    "\n",
    "        return X"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
